package com.cleaningcloud.cld.service.impl;

import com.cleaningcloud.cld.domain.FinanceReportBI;
import com.cleaningcloud.cld.domain.vo.CldBiDataVo;
import com.cleaningcloud.cld.mapper.FinanceReportBIMapper;
import com.cleaningcloud.cld.service.IFinanceReportBIService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

/**
 * @className 营运报表BI业务实现类
 * @description //TODO
 * @author Shaw
 * @date: 2025-03-06 16:33
 * @param: //TODO
 */
@Service
public class FinanceReportServiceBIImpl implements IFinanceReportBIService {

    //系统报表映射
    @Autowired
    private FinanceReportBIMapper financeReportBIMapper;


    //  ====================================合同金额版块====================================
    /**
     * @Author Shaw
     * @Description 合同金额
     * @date: 2025-04-09 12:04
     * @param: //TODO
     */
    @Override
    public List<FinanceReportBI> getFinanceReportBIContractMoney(FinanceReportBI financeReportBI) {
        return financeReportBIMapper.getFinanceReportBIContractMoney(financeReportBI);
    }

    //  ====================================合同金额下钻====================================
    /**
     * @Author Shaw
     * @Description 按区域查询合同金额占比
     * @date: 2025-03-06 16:37
     * @param: //TODO
     */
    @Override
    public List<FinanceReportBI> getFinanceReportBIContractMoneyByRegionRate(FinanceReportBI financeReportBI) {
        return financeReportBIMapper.getFinanceReportBIContractMoneyByRegionRate(financeReportBI);
    }

    /**
     * @Author Shaw
     * @Description 合同金额列表
     * @date: 2025-04-22 16:28
     * @param: //TODO
     */
    @Override
    public List<FinanceReportBI> getFinanceReportBIProjectContractMoneyList(FinanceReportBI financeReportBI){
        return financeReportBIMapper.getFinanceReportBIProjectContractMoneyList(financeReportBI);
    }

    //  ====================================工资版块====================================
    /**
     * @Author Shaw
     * @Description 工资版块
     * @date: 2025-03-06 16:37
     * @param: //TODO
     */
    @Override
    public Map<String, Object> getFinanceReportBISalarySection(FinanceReportBI financeReportBI) {
        return financeReportBIMapper.getFinanceReportBISalarySection(financeReportBI);
    }

    //  ====================================工资版块下钻====================================
    /**
     * @Author Shaw
     * @Description 按当月查询实际工资(日走势图)
     * @date: 2025-03-06 16:37
     * @param: //TODO
     */
    @Override
    public List<CldBiDataVo> getFinanceReportBIDownByMonthSalary(FinanceReportBI financeReportBI) {
        List<CldBiDataVo> arrlist = new ArrayList<>();
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1;
        int days = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        CldBiDataVo cv_pv = new CldBiDataVo();
        cv_pv.setDataName("计划值");
        BigDecimal[] pvAttr = new BigDecimal[days];

        CldBiDataVo cv_rv = new CldBiDataVo();
        cv_rv.setDataName("参考值");
        BigDecimal[] rvAttr = new BigDecimal[days];

        CldBiDataVo cv_gz = new CldBiDataVo();
        cv_gz.setDataName("基本工资");
        BigDecimal[] gzAttr = new BigDecimal[days];

        CldBiDataVo cv_jl = new CldBiDataVo();
        cv_jl.setDataName("经理津贴");
        BigDecimal[] jlAttr = new BigDecimal[days];

        CldBiDataVo cv_zg = new CldBiDataVo();
        cv_zg.setDataName("主管津贴");
        BigDecimal[] zgAttr = new BigDecimal[days];

        CldBiDataVo cv_lb = new CldBiDataVo();
        cv_lb.setDataName("领班津贴");
        BigDecimal[] lbAttr = new BigDecimal[days];

        CldBiDataVo cv_jb = new CldBiDataVo();
        cv_jb.setDataName("加班费");
        BigDecimal[] jbAttr = new BigDecimal[days];
        for (int i=1 ; i<=days ; i++) {
            String dataVal = "";
            if(i < 10){
                if(month < 10){
                    dataVal = year+"0"+month+"0"+i;
                }else{
                    dataVal = year+""+month+"0"+i;
                }
            }else{
                if(month < 10){
                    dataVal = year+"0"+month+""+i;
                }else{
                    dataVal = year+""+month+""+i;
                }
            }
            financeReportBI.setDateVal(dataVal);
            FinanceReportBI obj = financeReportBIMapper.getFinanceReportBIDownByMonthSalary(financeReportBI);

            pvAttr[i-1] = obj.getPv();
            rvAttr[i-1] = obj.getRv();
            gzAttr[i-1] = obj.getAvSalary();
            jlAttr[i-1] = obj.getHandleAllowance();
            zgAttr[i-1] = obj.getManagerAllowance();
            lbAttr[i-1] = obj.getForemanAllowance();
            jbAttr[i-1] = obj.getOverTimeCost();
        }
        cv_pv.setDataVal(pvAttr);
        cv_rv.setDataVal(rvAttr);
        cv_gz.setDataVal(gzAttr);
        cv_jl.setDataVal(jlAttr);
        cv_zg.setDataVal(zgAttr);
        cv_lb.setDataVal(lbAttr);
        cv_jb.setDataVal(jbAttr);
        arrlist.add(cv_pv);
        arrlist.add(cv_rv);
        arrlist.add(cv_gz);
        arrlist.add(cv_jl);
        arrlist.add(cv_zg);
        arrlist.add(cv_lb);
        arrlist.add(cv_jb);
        return arrlist;
    }

    /**
     * @Author Shaw
     * @Description 按当月查询实际工资(日期列表)
     * @date: 2025-03-06 16:37
     * @param: //TODO
     */
    @Override
    public FinanceReportBI getFinanceReportBIDownByMonthSalaryList() {
        FinanceReportBI bi = new FinanceReportBI();
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1;
        int days = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        //参考值
        List<BigDecimal> rvList = new ArrayList<>();
        BigDecimal rvSumVal = new BigDecimal(0);
        FinanceReportBI rvAttr = new FinanceReportBI();
        rvAttr.setSelectType(1);
        //计划值
        List<BigDecimal> pvList = new ArrayList<>();
        BigDecimal pvSumVal = new BigDecimal(0);
        FinanceReportBI pvAttr = new FinanceReportBI();
        pvAttr.setSelectType(2);
        //实际值
        List<BigDecimal> avList = new ArrayList<>();
        BigDecimal avSumVal = new BigDecimal(0);
        //基本工资
        List<BigDecimal> gzList = new ArrayList<>();
        BigDecimal gzSumVal = new BigDecimal(0);
        FinanceReportBI gzAttr = new FinanceReportBI();
        gzAttr.setSelectType(3);
        //领班津贴
        List<BigDecimal> lbList = new ArrayList<>();
        BigDecimal lbSumVal = new BigDecimal(0);
        FinanceReportBI lbAttr = new FinanceReportBI();
        lbAttr.setSelectType(4);
        //主管津贴
        List<BigDecimal> zgList = new ArrayList<>();
        BigDecimal zgSumVal = new BigDecimal(0);
        FinanceReportBI zgAttr = new FinanceReportBI();
        zgAttr.setSelectType(5);
        //经理津贴
        List<BigDecimal> jlList = new ArrayList<>();
        BigDecimal jlSumVal = new BigDecimal(0);
        FinanceReportBI jlAttr = new FinanceReportBI();
        jlAttr.setSelectType(6);
        //加班费
        List<BigDecimal> jbList = new ArrayList<>();
        BigDecimal jbSumVal = new BigDecimal(0);
        FinanceReportBI jbAttr = new FinanceReportBI();
        jbAttr.setSelectType(7);
        for (int i=1 ; i<=days ; i++) {
            //参考值
            FinanceReportBI rv = financeReportBIMapper.getFinanceReportBIDownByMonthSalaryList(rvAttr);
            rvList.add(rv.getRv());
            rvSumVal = rvSumVal.add(rv.getRv());
            //计划值
            FinanceReportBI pv = financeReportBIMapper.getFinanceReportBIDownByMonthSalaryList(pvAttr);
            pvList.add(pv.getPv());
            pvSumVal = pvSumVal.add(pv.getPv());
            //基本工资
            FinanceReportBI gz = financeReportBIMapper.getFinanceReportBIDownByMonthSalaryList(gzAttr);
            if(null != gz){
                gzList.add(null == gz.getAvSalary() ? new BigDecimal(0) :gz.getAvSalary());
                gzSumVal = gzSumVal.add(gz.getAvSalary());
            }else{
                gzList.add(new BigDecimal(0));
            }
            //领班津贴
            FinanceReportBI lb = financeReportBIMapper.getFinanceReportBIDownByMonthSalaryList(lbAttr);
            if(null != lb){
                lbList.add(null == lb.getForemanAllowance() ? new BigDecimal(0) :lb.getForemanAllowance());
                lbSumVal = lbSumVal.add(lb.getForemanAllowance());
            }else{
                lbList.add(new BigDecimal(0));
            }
            //主管津贴
            FinanceReportBI zg = financeReportBIMapper.getFinanceReportBIDownByMonthSalaryList(zgAttr);
            if(null != zg){
                zgList.add(null == zg.getManagerAllowance() ? new BigDecimal(0) :zg.getManagerAllowance());
                zgSumVal = zgSumVal.add(zg.getManagerAllowance());
            }else{
                zgList.add(new BigDecimal(0));
            }
            //经理津贴
            FinanceReportBI jl = financeReportBIMapper.getFinanceReportBIDownByMonthSalaryList(jlAttr);
            if(null != jl){
                jlList.add(null == jl.getHandleAllowance() ? new BigDecimal(0) :jl.getHandleAllowance());
                jlSumVal = jlSumVal.add(jl.getHandleAllowance());
            }else{
                jlList.add(new BigDecimal(0));
            }
            //加班费
            FinanceReportBI jb = financeReportBIMapper.getFinanceReportBIDownByMonthSalaryList(jbAttr);
            if(null != jb){
                jbList.add(null == jb.getOverTimeCost() ? new BigDecimal(0) :jb.getOverTimeCost());
                jbSumVal = jbSumVal.add(jb.getOverTimeCost());
            }else{
                jbList.add(new BigDecimal(0));
            }
            //实际值
            BigDecimal av = new BigDecimal(0);
            if(null != gz){
                av.add(null == gzList.get(i-1) ? new BigDecimal(0) : gzList.get(i-1));
            }
            if(null != lb){
                av.add(null == lbList.get(i-1) ? new BigDecimal(0) : lbList.get(i-1));
            }
            if(null != zg){
                av.add(null == zgList.get(i-1) ? new BigDecimal(0) : zgList.get(i-1));
            }
            if(null != jl){
                av.add(null == jlList.get(i-1) ? new BigDecimal(0) : jlList.get(i-1));
            }
            if(null != jb){
                av.add(null == jbList.get(i-1) ? new BigDecimal(0) : jbList.get(i-1));
            }
            avList.add(av);
            avSumVal = avSumVal.add(av);
        }
        rvList.add(rvSumVal);
        pvList.add(pvSumVal);
        avList.add(avSumVal);
        gzList.add(gzSumVal);
        lbList.add(lbSumVal);
        zgList.add(zgSumVal);
        jlList.add(jlSumVal);
        jbList.add(jbSumVal);

        bi.setRvList(rvList);
        bi.setPvList(pvList);
        bi.setAvList(avList);
        bi.setSalaryList(gzList);
        bi.setLbAllowanceList(lbList);
        bi.setZgAllowanceList(zgList);
        bi.setJlAllowanceList(jlList);
        bi.setOverTimeList(jbList);
        return bi;
    }

    /**
     * @Author Shaw
     * @Description 按区域实际工资占比
     * @date: 2025-03-06 16:37
     * @param: //TODO
     */
    @Override
    public List<FinanceReportBI> getFinanceReportBIDownByMonthSalaryRate(FinanceReportBI financeReportBI) {
        return financeReportBIMapper.getFinanceReportBIDownByMonthSalaryRate(financeReportBI);
    }

    //  ====================================物资+资产版块====================================
    /**
     * @Author Shaw
     * @Description 物资+资产版块
     * @date: 2025-03-06 16:37
     * @param: //TODO
     */
    @Override
    public Map<String, Object> getFinanceReportBIMaterialSection(FinanceReportBI financeReportBI){
        return financeReportBIMapper.getFinanceReportBIMaterialSection(financeReportBI);
    }

    //  ====================================税费+抵扣版块====================================
    /**
     * @Author Shaw
     * @Description 税费+抵扣版块
     * @date: 2025-04-09 11:57
     * @param: //TODO
     */
    @Override
    public Map<String, Object> getFinanceReportBITaxSection(FinanceReportBI financeReportBI){
        return financeReportBIMapper.getFinanceReportBITaxSection(financeReportBI);
    }

    //  ====================================其他费用版块====================================
    /**
     * @Author Shaw
     * @Description 其他费用版块
     * @date: 2025-04-09 11:57
     * @param: //TODO
     */
    @Override
    public Map<String, Object> getFinanceReportBIExpenditureSection(FinanceReportBI financeReportBI){
        return financeReportBIMapper.getFinanceReportBIExpenditureSection(financeReportBI);
    }

    //  ====================================利润版块====================================
    /**
     * @Author Shaw
     * @Description 按当月查询日利润(日走势图)
     * @date: 2025-03-06 16:37
     * @param: //TODO
     */
    @Override
    public List<CldBiDataVo> getFinanceReportBIDataByProfit(FinanceReportBI financeReportBI) {
        List<CldBiDataVo> arrlist = new ArrayList<>();
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1;
        int days = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        for (int i=1 ; i<=days ; i++) {
            String dataVal = "";
            if(i < 10){
                if(month < 10){
                    dataVal = year+"0"+month+"0"+i;
                }else{
                    dataVal = year+""+month+"0"+i;
                }
            }else{
                if(month < 10){
                    dataVal = year+"0"+month+""+i;
                }else{
                    dataVal = year+""+month+""+i;
                }
            }
            financeReportBI.setDateVal(dataVal);
            CldBiDataVo obj = financeReportBIMapper.getFinanceReportBIDataByProfit(financeReportBI);
            obj.setDataName(String.valueOf(i));
            arrlist.add(obj);
        }
        return arrlist;
    }

    /**
     * @Author Shaw
     * @Description 利润版块
     * @date: 2025-04-09 11:57
     * @param: //TODO
     */
    @Override
    public Map<String, Object> getFinanceReportBIProfitSection(FinanceReportBI financeReportBI){
        return financeReportBIMapper.getFinanceReportBIProfitSection(financeReportBI);
    }

    //  ====================================按公司查询账户信息====================================
    /**
     * @Author Shaw
     * @Description 按公司查询账户信息
     * @date: 2025-04-22 16:28
     * @param: //TODO
     */
    @Override
    public List<FinanceReportBI> getFinanceReportBIAccountInfoList(FinanceReportBI financeReportBI){
        return financeReportBIMapper.getFinanceReportBIAccountInfoList(financeReportBI);
    }

    //  ====================================应收+实收版块====================================
    /**
     * @Author Shaw
     * @Description 应收+实收版块
     * @date: 2025-04-22 16:28
     * @param: //TODO
     */
    @Override
    public FinanceReportBI getFinanceReportBIReceivableAndReceipts(FinanceReportBI financeReportBI){
        return financeReportBIMapper.getFinanceReportBIReceivableAndReceipts(financeReportBI);
    }


//    /**
//     * @Author Shaw
//     * @Description 按项目查询经纬度
//     * @date: 2025-03-06 16:37
//     * @param: //TODO
//     */
//    @Override
//    public List<CldBiDataVo> getFinanceReportBIDataByCoordinate(FinanceReport financeReport) {
//        List<CldBiDataVo> cbdvlist = new ArrayList<>();
//        List<FinanceReport> listfr = financeReportBIMapper.getFinanceReportBIDataByCoordinate(financeReport);
//        if(!listfr.isEmpty()){
//            for (FinanceReport fr : listfr) {
//                CldBiDataVo obj = new CldBiDataVo();
//                obj.setDataId(fr.getParamsId());
//                obj.setDataName(fr.getParamsName());
//                BigDecimal[] attr = new BigDecimal[2];
//                attr[0] = fr.getLongitude();
//                attr[1] = fr.getLatitude();
//                obj.setCoordinate(attr);
//                cbdvlist.add(obj);
//            }
//        }
//        return cbdvlist;
//    }
}
